// import com.alibaba.druid.sql.ast.SQLObject;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.Collections;
// import java.util.List;

#include "SQLTinyIntExpr.h"

#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/StringUtils.h"

SQLTinyIntExpr::SQLTinyIntExpr()
{
}

SQLTinyIntExpr::SQLTinyIntExpr(uint16_t value)
    : SQLNumericLiteralExpr()
{

  this->value = value;
}

SQLTinyIntExpr::SQLTinyIntExpr(string_ptr value)
    : SQLNumericLiteralExpr()
{

  if (StringUtils::startWith(value, make_string_ptr("--")))
  {
    value = make_string_ptr(value->substr(2));
  }
  this->value = atoi(value->c_str());
}

SQLObject_ptr SQLTinyIntExpr::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<SQLTinyIntExpr>(new SQLTinyIntExpr(value));
  std::shared_ptr<SQLTinyIntExpr> x = std::dynamic_pointer_cast<SQLTinyIntExpr>(clone_tmp);
  return x;
}

// @Override
SQLObject_list_ptr SQLTinyIntExpr::getChildren()
{
  return SQLObject_list_ptr();
}

// @Override
double SQLTinyIntExpr::getNumber()
{
  return value;
}

// @Override
uint16_t SQLTinyIntExpr::getValue()
{
  return value;
}

void SQLTinyIntExpr::setValue(uint16_t value)
{
  this->value = value;
}

void SQLTinyIntExpr::accept0(SQLASTVisitor_ptr visitor)
{
  visitor->visit(SharedObject(SQLTinyIntExpr));

  visitor->endVisit(SharedObject(SQLTinyIntExpr));
}

// @Override
int SQLTinyIntExpr::hashCode()
{
  int prime = 31;
  int result = 1;
  // result = prime * result + ((value == null) ? 0 : value.hashCode());
  result = prime * result + value;
  return result;
}

// @Override
bool SQLTinyIntExpr::equals(Object_ptr obj)
{
  if (this == obj.get())
  {
    return true;
  }
  if (obj == nullptr)
  {
    return false;
  }
  if (getClass() != obj->getClass())
  {
    return false;
  }
  std::shared_ptr<SQLTinyIntExpr> other = std::dynamic_pointer_cast<SQLTinyIntExpr>(obj);
  // if (value == null) {
  //     if (other.value != null) {
  //         return BOOL::FALSE;
  //     }
  // } else
  if (!(value == other->value))
  {
    return false;
  }
  return true;
}

// @Override
void SQLTinyIntExpr::setNumber(double number)
{
  // if (number == null) {
  //     this->setValue(null);
  //     return;
  // }

  this->setValue(((uint16_t)number));
}
